<template>
  <el-dialog title="产品BOM物料选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
    <el-table
      v-loading="loading"
      :data="bomList"
      border
      @selection-change="handleSelectionChange"
      @cell-mouse-enter="handleCellEnter()"
      @cell-mouse-leave="handleCellLeave()">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label="物料编码" align="center" prop="itemCode" />
      <el-table-column label="物料名称" align="center" prop="itemName" :show-overflow-tooltip="true" />
      <el-table-column label="规格型号" align="center" prop="specification" :show-overflow-tooltip="true" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="使用量" align="center" prop="used">
        <div class="item" slot-scope="scope">
          <el-input-number
            class="item_input"
            v-model="scope.row.used"
            size="mini"
            :min="1"
            :max="scope.row.used"
            :step="1"
            style="width: 100%;"
            :title="scope.row.used"
          ></el-input-number>
        </div>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="model" />
      <el-table-column label="规格单位" align="center" prop="specificationUnit" />
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>

</template>

<script>
import { getConfirmItem } from "@/api/mes/md/bomItem";
import {
  addRouteproductbom, listRouteproductbom, getByBomId
} from '@/api/mes/pro/routeproductbom';

export default {
  name: "ItemBomSelectSingle",
  props: {
    bomId: null,
  },
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,
      selectedItemId: undefined,
      selectedRows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品BOM关系表格数据
      bomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemId: null,
        bomItemId: null,
        bomItemCode: null,
        bomItemName: null,
        bomItemSpec: null,
        unitOfMeasure: null,
        quantity: null,
        enableFlag: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
      },
      editProp: ['used'],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品BOM关系列表 */
    async getList() {
      this.loading = true;
      try {
        const confirmItemResponse = await getConfirmItem(this.bomId);
        const listResponse = await getByBomId(this.bomId);

        let rows = confirmItemResponse.rows;
        let list = listResponse.data;

        for (let i = 0; i < rows.length; i++) {
          for (let j = 0; j < list.length; j++) {
            if (rows[i].itemId === list[j].itemId
              && rows[i].model === list[j].model
              && rows[i].specificationUnit === list[j].specificationUnit) {
              if ((rows[i].used - list[j].used) <= 0) {
                rows.splice(i, 1);
                i--;
                break;
              } else {
                rows[i].used = rows[i].used - list[j].used;
              }
            }
          }
        }

        this.bomList = rows;
        this.total = confirmItemResponse.total;
        this.loading = false;
      } catch (error) {
        console.error('Error fetching data:', error);
        this.loading = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRows = row;
      }
    },
    // 多选选中数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.selectedRows= val
    },
    // 确定选中
    confirmSelect(){
      if(this.selectedRows.length <= 0){
        this.$notify({
          title:'提示',
          type:'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected',this.selectedRows);
      this.showFlag = false;
    },
    // 列表单元格鼠标进入事件
    handleCellEnter (row, column, cell, event) {
      if (column) {
        const property = column.property
        if (this.editProp.includes(property)) {
          cell.querySelector('.item_input').style.display = 'block'
        }
      }
    },
    // 列表单元格鼠标离开事件
    handleCellLeave (row, column, cell, event) {
      if (column) {
        const property = column.property
        if (this.editProp.includes(property)) {
          cell.querySelector('.item_input').style.display = 'none'
        }
      }
    },
  }
};
</script>
